package com.jmhy.sdk.fragment;

import android.Manifest;
import android.app.Fragment;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.os.Build;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Handler;
import android.os.Message;
import android.text.InputType;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.jmhy.sdk.activity.PermissionActivity;
import com.jmhy.sdk.adapter.UserAccount8Adapter;
import com.jmhy.sdk.common.JiMiSDK;
import com.jmhy.sdk.config.AppConfig;
import com.jmhy.sdk.http.ApiAsyncTask;
import com.jmhy.sdk.http.ApiRequestListener;
import com.jmhy.sdk.model.Guest;
import com.jmhy.sdk.model.LoginMessage;
import com.jmhy.sdk.sdk.JmhyApi;
import com.jmhy.sdk.utils.FragmentUtils;
import com.jmhy.sdk.utils.MediaUtils;
import com.jmhy.sdk.utils.Seference;
import com.jmhy.sdk.utils.Utils;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class JmUserSetGuestPswFragment8 extends JmBaseFragment implements View.OnClickListener {
    private EditText et_input_account, et_input_pwd;

    private TextView tv_screen_save_tip;
    private Button btn_login;


    private String path;
    private File file;
    private String url;

    private String password;
    private String result;
    private String msg;
    private String uname;
    private String gametoken;
    private String openid;

    private View contentView;

    private TimeCount time;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (getArguments() != null) {
            uname = getArguments().getString("username");
            password = getArguments().getString("upass");
            msg = getArguments().getString("msg");
            gametoken = getArguments().getString("gametoken");
            openid = getArguments().getString("openid");
            url = getArguments().getString("url");
        }
        View view = null;
        view = inflater.inflate(AppConfig.resourceId(getActivity(), "jm_guest_set_pwd_8", "layout"), container, false);
        view.setClickable(true);
        return view;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView();
    }

    private void initView() {

        et_input_account = getView().findViewById(AppConfig.resourceId(getActivity(), "et_input_account", "id"));

        et_input_account.setEnabled(false);


        et_input_pwd = getView().findViewById(AppConfig.resourceId(getActivity(), "et_input_pwd", "id"));

        et_input_pwd.setEnabled(false);


        et_input_account.setText(uname);
        et_input_pwd.setText(password);

        tv_screen_save_tip = getView().findViewById(
                AppConfig.resourceId(getActivity(), "tv_screen_save_tip", "id"));

        btn_login = getView().findViewById(
                AppConfig.resourceId(getActivity(), "btn_login", "id"));
        btn_login.setOnClickListener(this);

        contentView = getView().findViewById(
                AppConfig.resourceId(getActivity(), "content_view", "id"));

        time = new TimeCount(50, 50);//控制弹窗显示时间
        time.start();
    }


    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == AppConfig.resourceId(getActivity(), "btn_login", "id")) {
            // 返回数据给游戏

            wrapaLoginInfo("success", msg, uname, openid, gametoken);

            turnToNotice(url);
            getActivity().finish();
            JiMiSDK.reportClipboard(getActivity());
        }


    }


    class TimeCount extends CountDownTimer {
        /**
         * 构造方法
         *
         * @param millisInFuture    总倒计时长 毫秒
         * @param countDownInterval 倒计时间隔
         */
        public TimeCount(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        @Override
        public void onTick(long millisUntilFinished) {

        }

        @Override
        public void onFinish() {// 计时结束
            saveCurrentImage();
        }
    }

    private void saveCurrentImage() {

        List<String> permission = new ArrayList<>();
        permission.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
        PermissionActivity.requestPermission(getActivity(), permission, new PermissionActivity.PermissionResultListener() {
            @Override
            public void onPermissionResult(boolean grant) {
                if (!grant) {
                    JiMiSDK.permissionTip(getActivity(), "jm_permission_tip_refuse_storage");
                } else {
                    saveCurrentSnapshot();
                }
            }
        });

    }

    /**
     * 截屏
     */
    private void saveCurrentSnapshot() {
        if (contentView == null) {
            return;
        }

        // 找到当前页面的跟布局
        contentView.setDrawingCacheEnabled(true);
        contentView.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH);
        Bitmap temBitmap = Bitmap.createBitmap(contentView.getWidth(), contentView.getHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(temBitmap);
        contentView.draw(canvas);
        contentView.destroyDrawingCache();
        Log.i("jimi测试", "getActivity:" + getActivity());
        if (getActivity() != null) {
            MediaUtils.saveImage(getActivity(), temBitmap);
            String snapshot = AppConfig.getString(getActivity(), "snapshot_save");
            showMsg(snapshot);
        }
    }
}
